<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class MessageController extends BaseController {

	public function index() {
		$data['message_client'] = array('IN','1,2');
		$data['message_type'] = -1;
		$data['message_display'] = 1;
		$count = M('message')->where($data)->count();
		$page = getpage($count,10);
		$message_arr = M('message')->where($data)->order('message_id desc')->limit($page->firstRow, $page->listRows)->select();
		$this->assign('message_arr',$message_arr);
		$this->assign('page', $page->show());
		$this->display();
	}

	public function message_details() {
		$message_arr = M('message')->where($_GET)->find();
		$this->assign('message_arr',$message_arr);
		$this->display();
	}

	public function message_del() {
		$data['message_display'] = 0;
		$data['message_id'] = $_GET['message_id'];
		M('message')->save($data);
		$this->redirect('index');
	}

	public function message_edit() {
		if (I('post.action') == 'message_edit_action') {
            $this->message_edit_action();
            exit();
        }
		$message_arr = M('message')->where($_GET)->find();
		$this->assign('message_arr',$message_arr);
		$this->display();
	}

	public function message_edit_action() {
		if (M('message')->save($_POST)) {
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
	}

	public function message_add() {
		if (I('post.action') == 'message_add_action') {
            $this->message_add_action();
            exit();
        }
		$this->display();
	}

	public function message_add_action() {
		if ($_POST != array_filter($_POST)) {
			$this->error('选项不能有空值！');
		}

		if ($_POST['message_client'] == 1) {//考生端
			$_POST['message_time'] = time();
			if ($msg_id = M('message')->add($_POST)) {
				//考生端广播
				push('all',$_POST['message_title'],C(JPUSH_USER),1,$msg_id);
				$this->redirect('index');
			}else{
				$this->error('消息发布失败！');
			}
		}elseif ($_POST['message_client'] == 2) {//考官端
			$_POST['message_time'] = time();
			if ($msg_id = M('message')->add($_POST)) {
				//考官端广播
				push('all',$_POST['message_title'],C(JPUSH_EXAMINER),1,$msg_id);
				$this->redirect('index');
			}else{
				$this->error('消息发布失败！');
			}
		}else{//客户端类型错误
			$this->error('客户端类型错误！');
		}
	}

}